package chap1;

import chap1.util.ChineseAndEnglishUtils;
import chap1.util.StringUtil;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.*;

public class ReadFile {
    public static void main(String[] args) {
//        test1();
//        testEng();
//        start();
//        testChinese();
//        testCnt();
        start();
    }

    static void start() {
        String fileContent = readFile();
        SplitEngThread readEngThread = new SplitEngThread("ReadEngThread", fileContent);
        readEngThread.run();


        SplitChineseThread splitChineseThread = new SplitChineseThread("SplitChineseThread", fileContent);
        splitChineseThread.run();


        CntWordsThread cntWordsThread = new CntWordsThread("CntWordsThread", fileContent);
        cntWordsThread.run();
    }

    static String readFile() {
        String inFilePath = "Z01-Example.txt";
//        String whole="";
        StringBuilder whole = new StringBuilder();
        try (Scanner scanner = new Scanner(new File(inFilePath))) {
            while (scanner.hasNextLine()) {
                String line = scanner.nextLine();
                whole.append(line).append("\n");
            }

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return whole.toString();

    }

    static void testEng() {

        String fileContent = readFile();
        Set<String> engs = new TreeSet<>();
//        for (int i = 0; i < 10; i++) {
//            System.out.println(name + "运行，i = " + i);
//        }
        StringBuilder tmpEng = new StringBuilder();
        for (int i = 0; i < fileContent.length(); i++) {
//            char c = fileContent.charAt(i);
//            if()
            char ch = fileContent.charAt(i);

//            if(StringUtil.isChinese(ch)){
//
//            }
            if (ch >= '0' && ch <= '9') {

//                digitCount++;

            } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {

//                engishCount++;

                tmpEng.append(ch);
            } else {

//                chineseCount++;
                String eng = tmpEng.toString();
                if (!eng.equals("")) {
//                    engs.add(tmpEng.toString());
                    engs.add(eng);

                }
                tmpEng = new StringBuilder();

            }
        }

        try (FileWriter fileWriter = new FileWriter("eng_dic.txt")) {
            fileWriter.write(String.valueOf(engs));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static int cntLine(String line) {
        List<String> engs = new ArrayList<>();
        List<Character> chineseLst = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            System.out.println(name + "运行，i = " + i);
//        }
//        String[] split = fileContent.split("\n");
//        for (String line : split) {
//
//        }
        StringBuilder tmpEng = new StringBuilder();
        for (int i = 0; i < line.length(); i++) {
//            char c = fileContent.charAt(i);
//            if()
            char ch = line.charAt(i);

//            if(StringUtil.isChinese(ch)){
//
//            }
            if (ch >= '0' && ch <= '9') {

//                digitCount++;

            } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {

//                engishCount++;

                tmpEng.append(ch);
            } else {

//                chineseCount++;
                String eng = tmpEng.toString();
                if (!eng.equals("")) {
//                    engs.add(tmpEng.toString());
                    engs.add(eng);

                }
                tmpEng = new StringBuilder();
                if (ChineseAndEnglishUtils.isChineseByBlock(ch)) {
//                chineseWords.add(ch);
                    chineseLst.add(ch);
//                    if(chineseWords.containsKey(ch)){
//                        chineseWords.put(ch,chineseWords.get(ch)+1);
//                    }else{
//                        chineseWords.put(ch,1);
//                    }
//                if(chineseWords.get(ch))
                }

            }
        }
        return chineseLst.size() + engs.size();
    }

    static void testCnt() {

        String fileContent = readFile();
//        Set<String> engs = new TreeSet<>();
//        Set<String> engs = new TreeSet<>();
//        List<String> engs = new ArrayList<>();
//        List<Character> chineseLst = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            System.out.println(name + "运行，i = " + i);
//        }
        String[] split = fileContent.split("\n");
        List<Integer> cntLines = new ArrayList<>();
        for (String line : split) {
            cntLines.add(cntLine(line));
        }

//
//        StringBuilder tmpEng = new StringBuilder();
//        for (int i = 0; i < fileContent.length(); i++) {
////            char c = fileContent.charAt(i);
////            if()
//            char ch = fileContent.charAt(i);
//
////            if(StringUtil.isChinese(ch)){
////
////            }
//            if (ch >= '0' && ch <= '9') {
//
////                digitCount++;
//
//            } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
//
////                engishCount++;
//
//                tmpEng.append(ch);
//            } else {
//
////                chineseCount++;
//                String eng = tmpEng.toString();
//                if (!eng.equals("")) {
////                    engs.add(tmpEng.toString());
//                    engs.add(eng);
//
//                }
//                tmpEng = new StringBuilder();
//                if (ChineseAndEnglishUtils.isChineseByBlock(ch)) {
////                chineseWords.add(ch);
//                    chineseLst.add(ch);
////                    if(chineseWords.containsKey(ch)){
////                        chineseWords.put(ch,chineseWords.get(ch)+1);
////                    }else{
////                        chineseWords.put(ch,1);
////                    }
////                if(chineseWords.get(ch))
//                }
//
//            }
//        }
        System.out.println("cntLines");
        System.out.println(cntLines);
        int cnt = 1;
        StringBuilder outStr = new StringBuilder();
        for (Integer cntLine : cntLines) {
            outStr.append(String.format("第%d行，有%d个单词\n", cnt, cntLine));
            cnt++;
//            System.out.println(String.format("第%d行，有%d个单词",cnt));
        }


        try (FileWriter fileWriter = new FileWriter("cnt_words.txt")) {
//            fileWriter.write(String.valueOf(engs));
            fileWriter.write(String.valueOf(outStr));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    static void testChinese() {

        String fileContent = readFile();
//        Set<String> engs = new TreeSet<>();
//        Set<String> chineseWords = new TreeSet<>();
//        Set<Character> chineseWords = new TreeSet<>();
        Map<Character, Integer> chineseWords = new TreeMap<>();
//        for (int i = 0; i < 10; i++) {
//            System.out.println(name + "运行，i = " + i);
//        }
//        StringBuilder tmpEng = new StringBuilder();
        for (int i = 0; i < fileContent.length(); i++) {
//            char c = fileContent.charAt(i);
//            if()
            char ch = fileContent.charAt(i);

//            if (StringUtil.isChinese(ch)) {
            if (ChineseAndEnglishUtils.isChineseByBlock(ch)) {
//                chineseWords.add(ch);
                if (chineseWords.containsKey(ch)) {
                    chineseWords.put(ch, chineseWords.get(ch) + 1);
                } else {
                    chineseWords.put(ch, 1);
                }
//                if(chineseWords.get(ch))
            }

//            if (ch >= '0' && ch <= '9') {
//
////                digitCount++;
//
//            } else if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) {
//
////                engishCount++;
//
////                tmpEng.append(ch);
//            } else {
//
////                chineseCount++;
//                chineseWords.add(ch);
////                String eng = tmpEng.toString();
////                if (!eng.equals("")) {
//////                    engs.add(tmpEng.toString());
//////                    engs.add(eng);
////                    chineseWords.add(ch);
////
////                }
////                tmpEng = new StringBuilder();
//
//            }
        }

        String writeStr = String.valueOf(chineseWords);
        System.out.println(writeStr);

        try (FileWriter fileWriter = new FileWriter("chineseWords_dic.txt")) {
//            fileWriter.write(String.valueOf(chineseWords));
            fileWriter.write(writeStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static void test1() {
        String string = "3131中国abc";
        for (int i = 0; i < string.length(); i++) {
            System.out.println(string.charAt(i));
        }

//       3
//       1
//       3
//       1
//       中
//               国
//       a
//               b
//       c
    }
}
